//
//  UIButton+Extension.swift
//  CFZWeiBo
//
//  Created by Cedric Zheng on 26/11/2016.
//  Copyright © 2016 Cedric Zheng. All rights reserved.
//

import UIKit

extension UIButton {
    
    convenience init (title: String?, fontSize: CGFloat = 13.0, color: UIColor = UIColor.darkGray, imageName: String? = nil, bgImageName: String? = nil, target: Any? = nil, selector: Selector? = nil, event: UIControlEvents = .touchUpInside) {
        
        self.init()
        
        if let title = title {
            setTitle(title, for: .normal)
            setTitleColor(color, for: .normal)
            titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        }
        
        if let imageName = imageName {
            setImage(UIImage(named: imageName), for: .normal)
            setImage(UIImage(named: "\(imageName)_highlighted"), for: .highlighted)
        }
        
        if let bgImageName = bgImageName {
            setBackgroundImage(UIImage(named: bgImageName), for: .normal)
            setBackgroundImage(UIImage(named: "\(bgImageName)_highlighted"), for: .highlighted)
        }
        
        if let target = target, let selector = selector {
            addTarget(target, action: selector, for: event)
        }
    }
}

